package com.thams.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MessagePrinter {
	public static void printErrorMessage(String message,HttpServletRequest request,HttpServletResponse response){
		printMessage(message, "fail",request, response);
	}
	public static void printSuccessMessage(String message,HttpServletRequest request,HttpServletResponse response){
		printMessage(message, "success",request, response);
	}
	public static void printObject(String message,HttpServletRequest request,HttpServletResponse response){
		String callback = request.getParameter("jsoncallback");
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(callback+"("+message+")");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static void printMessage(String message,String status,HttpServletRequest request,HttpServletResponse response){
		String callback = request.getParameter("jsoncallback");
		response.setHeader("Cache-Control", "no-cache");
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(callback+"({\"status\":\""+status+"\",\"message\":\""+message+"\"})");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
